import 'package:flutter/material.dart';
import 'package:industry_net/base/component/list/section.dart';

class TableMan {
  int count = 0;
  List<Widget> items = [];

  void getCount(List<SectionModel> data,
      {bool hasHeader = false, bool hasFooter = false}) {
    if (hasHeader) {
      count += 1;
    }
    if (hasFooter) {
      count += 1;
    }
    if (data.isEmpty) {
      return;
    }
    for (var item in data) {
      count += item.items.length;
      if (item.sectionHeader != null) {
        count++;
      }
      if (item.sectionFooter != null) {
        count++;
      }
    }
  }

  void initItems(BuildContext context, List<SectionModel> data, Widget? header,
      Widget? footer) {
    if (items.isEmpty) {
      if (header != null) {
        items.add(header);
      }
      for (var i = 0; i < data.length; i++) {
        var sec = data[i];
        if (sec.sectionHeader != null) {
          items.add(sec.sectionHeader!);
        }
        for (var j = 0; j < sec.items.length; j++) {
          var item = sec.itemBuilder(context, j);
          items.add(item);
        }
        if (sec.sectionFooter != null) {
          items.add(sec.sectionFooter!);
        }
      }
      if (footer != null) {
        items.add(footer);
      }
    }
  }
}
